package com.pro.controller;

import com.pro.pojo.JsComment;
import com.pro.pojo.JsProduct;
import com.pro.pojo.JsonData;
import com.pro.service.JsCommentGoodsService;
import com.pro.utils.DateUtil;
import com.pro.utils.SensitiveWordsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ClassName:JsCommentGoodsController
 * Package:com.pro.controller
 * Description:
 *
 * @Date:2021/11/9 14:06
 * @Author: LBF
 */
@Controller
@RequestMapping("comment/goods")
public class JsCommentGoodsController {

    @Autowired
    private JsCommentGoodsService jsCommentServer;

    //商品类型为3
    @RequestMapping("/getFrontEndCommentGoods")
    @ResponseBody
    public JsonData getFrontEndCommentGoods(String status){
        status = !("null").equals(status) && status != null ? "%"+status+"%":"%%";
        List<JsComment> commentGoods = jsCommentServer.getFrontEndComment("%%",status);
        Integer commentGoodsCount = jsCommentServer.getFrontEndCommentCount("%%",status);
        //进行敏感词过滤
        commentGoods = SensitiveWordsUtil.getListFilter(commentGoods);
        return commentGoodsCount!=null ? JsonData.buildSuccess(commentGoods,commentGoodsCount): JsonData.buildError("服务端错误");
    }

    @RequestMapping("/getFrontEndCommentGoodsCount")
    @ResponseBody
    public JsonData getFrontEndCommentGoodsCount(String status){
        status = !("null").equals(status) && status != null ? "%"+status+"%":"%%";
        List<JsComment> commentGoods = new ArrayList<>();
        commentGoods.add(new JsComment());
        commentGoods.get(0).setNormal(jsCommentServer.getFrontEndStatusCount("1"));
        commentGoods.get(0).setIntercept(jsCommentServer.getFrontEndStatusCount("2"));
        commentGoods.get(0).setRemove(jsCommentServer.getFrontEndStatusCount("3"));
        commentGoods.get(0).setAll(commentGoods.get(0).getNormal()+commentGoods.get(0).getIntercept()+commentGoods.get(0).getRemove());
        String date = "%%";
        commentGoods.get(0).setCommentSum(jsCommentServer.getFrontEndCommentCount(date,status));
        commentGoods.get(0).setCommentPeopleSum(jsCommentServer.getFrontEndCommentPeopleNumbers(date,status));
        date = "";
        date = "%"+ DateUtil.transDate(new Date())+"%";
        commentGoods.get(0).setCommentTodaySum(jsCommentServer.getFrontEndCommentCount(date,status));
        commentGoods.get(0).setCommentTodayPeopleSum(jsCommentServer.getFrontEndCommentPeopleNumbers(date,status));
        return commentGoods!=null ? JsonData.buildSuccess(commentGoods): JsonData.buildError("服务端错误");
    }

    @RequestMapping("/getBackstageCommentGoods")
    @ResponseBody
    public JsonData getBackstageCommentGoods(String status){
        status = !("null").equals(status) && status != null ? "%"+status+"%":"%%";
        List<JsComment> commentGoods = jsCommentServer.getBackstageComment("%%",status);
        Integer commentGoodsCount = jsCommentServer.getBackstageCommentCount("%%",status);
        //进行敏感词过滤
        commentGoods = SensitiveWordsUtil.getListFilter(commentGoods);
        return commentGoodsCount!=null ? JsonData.buildSuccess(commentGoods,commentGoodsCount): JsonData.buildError("服务端错误");
    }

    @RequestMapping("/getBackstageCommentGoodsCount")
    @ResponseBody
    public JsonData getBackstageCommentGoodsCount(String status){
        status = !("null").equals(status) && status != null ? "%"+status+"%":"%%";
        List<JsComment> commentGoods = new ArrayList<>();
        commentGoods.add(new JsComment());
        commentGoods.get(0).setNormal(jsCommentServer.getBackstageStatusCount("1"));
        commentGoods.get(0).setIntercept(jsCommentServer.getBackstageStatusCount("2"));
        commentGoods.get(0).setRemove(jsCommentServer.getBackstageStatusCount("3"));
        commentGoods.get(0).setAll(commentGoods.get(0).getNormal()+commentGoods.get(0).getIntercept()+commentGoods.get(0).getRemove());
        String date = "%%";
        commentGoods.get(0).setCommentSum(jsCommentServer.getBackstageCommentCount(date,status));
        commentGoods.get(0).setCommentPeopleSum(jsCommentServer.getBackstageCommentPeopleNumbers(date,status));
        date = "";
        date = "%"+ DateUtil.transDate(new Date())+"%";
        commentGoods.get(0).setCommentTodaySum(jsCommentServer.getBackstageCommentCount(date,status));
        commentGoods.get(0).setCommentTodayPeopleSum(jsCommentServer.getBackstageCommentPeopleNumbers(date,status));
        return commentGoods!=null ? JsonData.buildSuccess(commentGoods): JsonData.buildError("服务端错误");
    }

    @RequestMapping("editCommentGoodsStatus")
    @ResponseBody
    public JsonData editCommentGoodsStatus(String id, String status){
        Integer i = 0;
        if (status.equals("拦截")){
            i = jsCommentServer.editCommentStatus(id, "2");
        }else if(status.equals("删除")){
            i = jsCommentServer.editCommentStatus(id, "3");
        }
        return i > 0 ? JsonData.buildSuccess(i): JsonData.buildError("服务端错误");
    }


    @RequestMapping("/getGoodsById")
    @ResponseBody
    public JsonData getGoodsById(Integer id){
        JsProduct jsProduct = jsCommentServer.getGoodsById(id);
        return jsProduct!=null ? JsonData.buildSuccess(jsProduct): JsonData.buildError("服务端错误");
    }


    @RequestMapping("getBeforeCommentById")
    @ResponseBody
    public JsonData getBeforeCommentById(Integer id){
        List<JsComment> comments = jsCommentServer.getBeforeCommentById(id);
        for (JsComment comment : comments) {
            List<JsComment> childrens = jsCommentServer.getBeforeCommentChildren((int) comment.getId());
            if (childrens!=null){
                comment.setChildren(childrens);
            }
        }
        //进行敏感词过滤
        comments = SensitiveWordsUtil.getListFilter(comments);
        return JsonData.buildSuccess(comments);
    }

    @RequestMapping("getCommentById")
    @ResponseBody
    public JsonData getCommentById(Integer id){
        List<JsComment> comments = jsCommentServer.getCommentById(id);
        for (JsComment comment : comments) {
            List<JsComment> childrens = jsCommentServer.getCommentChildren((int) comment.getId());
            if (childrens!=null){
                System.out.println(childrens);
                comment.setChildren(childrens);
            }
        }
        //进行敏感词过滤
        comments = SensitiveWordsUtil.getListFilter(comments);
        return JsonData.buildSuccess(comments);
    }


    @RequestMapping("addComment")
    @ResponseBody
    public JsonData addComment(String userId, String id, String content, String createDate, String parentId){
        JsComment jsComment = null;
        Integer i = 0;
        try {
            jsComment = new JsComment(Integer.valueOf(userId),Integer.valueOf(id),3,content,Integer.valueOf(parentId), DateUtil.transString(createDate),2,"1","2",String.valueOf(userId), DateUtil.transString(createDate));
            i = jsCommentServer.addComment(jsComment);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return i > 0 ? JsonData.buildSuccess(jsComment): JsonData.buildError("服务端错误");
    }


    @RequestMapping("editCommentDisplay")
    @ResponseBody
    public JsonData editCommentDisplay(Integer id, String display){
        Integer i = jsCommentServer.editCommentDisplay(id, display);
        return i > 0 ? JsonData.buildSuccess(i) : JsonData.buildError("服务端错误");
    }

}
